package com.gl.util;

import java.io.*;
import java.util.Map;

public class BuffeTest {
    //解密
    public static void decryptFile(File decryptedFile, File newFile) throws IOException {
        try (FileInputStream fileInputStream = new FileInputStream(decryptedFile);
             FileOutputStream fileOutputStream = new FileOutputStream(newFile);
             BufferedInputStream inputStream = new BufferedInputStream(fileInputStream);
             BufferedOutputStream outputStream = new BufferedOutputStream(fileOutputStream)) {

            int b;
            while ((b = inputStream.read()) != -1) {
                //与5进行异或
                outputStream.write(b ^ 15511);
            }
            outputStream.flush(); //刷新流
        }
        //所有流都会在try-with-resources中自动关闭
    }

    /**
     * @param decryptedFile 原图
     * @param imgMap        全局map
     * @throws IOException
     */
    public static void decryptFileTOByte(File decryptedFile, Map<String, byte[]> imgMap) throws IOException {
        try (FileInputStream fileInputStream = new FileInputStream(decryptedFile);
             BufferedInputStream inputStream = new BufferedInputStream(fileInputStream);
             ByteArrayOutputStream bout = new ByteArrayOutputStream()) {

            int b;
            while ((b = inputStream.read()) != -1) {
                //与5进行异或
                bout.write(b ^ 15511);
            }
            bout.flush(); //刷新流
            imgMap.put(decryptedFile.getName(), bout.toByteArray());
        }
        //所有流都会在try-with-resources中自动关闭
    }

    //加密
    public static void encryptedFile(File oldFile, File newFile) throws IOException {
        try (FileInputStream fileInputStream = new FileInputStream(oldFile);
             FileOutputStream fileOutputStream = new FileOutputStream(newFile);
             BufferedInputStream inputStream = new BufferedInputStream(fileInputStream);
             BufferedOutputStream outputStream = new BufferedOutputStream(fileOutputStream)) {

            int b;
            while ((b = inputStream.read()) != -1) {
                //与5进行异或
                outputStream.write(b ^ 15511);
            }
            outputStream.flush(); //刷新流
        }
        //所有流都会在try-with-resources中自动关闭
    }

    public static void main(String[] args) throws IOException {
        // 示例代码
    }
}
